package com.jr.controller;

import com.google.gson.Gson;
import com.jr.service.IBookManagerService;
import com.jr.service.impl.BookManagerServiceImpl;
import com.jr.util.PageHelper;
import com.jr.vo.ShowBook;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/bookshow/*")
public class ShowBookInfo extends HttpServlet {

    IBookManagerService ibms = new BookManagerServiceImpl();

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // super.service(req, resp); //只根据【请求方式】分发；但是现在需要根据【请求路径】分发
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        Class cla = this.getClass();
        Method method = null;
        try {
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            method.invoke(this, request, response);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (
                InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public void showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PageHelper ph = new PageHelper();
        String booktype = request.getParameter("booktype");
        String bookname =request.getParameter("bookname");
        String isborrow =request.getParameter("isborrow");


        ShowBook sb =new ShowBook();
        sb.setBooktype(Integer.parseInt(booktype));
        sb.setBookname(bookname);
        sb.setIsborrow(Integer.parseInt(isborrow));



        ph.setPageIndex(Integer.parseInt(request.getParameter("pageindex")));
        ph.setDateCount(ibms.queryBookCount(sb));

        ph.setListIndex(ibms.queryByBook(sb,ph));

        response.getWriter().println(new Gson().toJson(ph));


    }
}
